
package org.kommii.mongodb;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * mongodb的配置文件类
 *	@author: zhb
 */
public class MongoDb_Config {
	/*
	 * mongodb 服务器的ip
	 */
	private static List<String> dbServerIP = new ArrayList<String>();
	/*
	 * mongodb 服务器的端口
	 */
    private static List<Integer> dbServerPort = new ArrayList<Integer>();;
    /*
     * mongodb 数据库名称
     */
    private static List<String> dbName = new ArrayList<String>();
    
    //每个数据库的实例个数
    private static List<Integer> connSize = new ArrayList<Integer>();;
    
    /*
     *  mongodb 数据库 用户名
     */
    private static List<String> dbUserName = new ArrayList<String>();
    /*
     *  mongodb 数据库 密码
     */
    private static List<String> dbPassword = new ArrayList<String>();
    
    /*
     *  每个主机对 mongodb 服务器的最大连接数
     */
    private static List<Integer> connectionsPerHost = new ArrayList<Integer>();
    
    /*
     * 每个连接所负载的线程队列数
     */
    private static List<Integer> threadsAllowedToBlockForConnectionMultiplier = new ArrayList<Integer>();
    
    /*
     * 连接超时时长
     */
    private static List<Integer> connectTimeout = new ArrayList<Integer>();
    /*
     * 是否自动重连
     */
    private static List<Boolean> autoConnectRetry = new ArrayList<Boolean>();
    
	static{
		try{
			Properties prop = new Properties();
			InputStream is = MongoDb_Config.class.getClassLoader().getResourceAsStream("mongodb_config.properties");
			prop.load(is);
			String regex = ":";
			String[] ips = prop.getProperty("dbServerIP").split(regex);
			
			for(int i=0;i<ips.length;i++){
				dbServerIP.add(ips[i].trim());
			}
			
			String[] ports = prop.getProperty("dbServerPort").split(regex);	
			for(int i=0;i<ports.length;i++){
				dbServerPort.add(Integer.parseInt(ports[i].trim()));
			}
			
			String[] dbnames = prop.getProperty("dbName").split(regex);
			for(int i=0;i<dbnames.length;i++){
				dbName.add(dbnames[i].trim());
			}
			
			String[] dbUserNames = prop.getProperty("dbUserName").split(regex);
			for(int i=0;i<dbUserNames.length;i++){
				dbUserName.add(dbUserNames[i].trim());
			}
			
			String[] dbPasswords = prop.getProperty("dbPassword").split(regex);
			for(int i=0;i<dbPasswords.length;i++){
				dbPassword.add(dbPasswords[i].trim());
			}
			
			String[] poolSizes = prop.getProperty("poolSize").split(regex);	
			for(int i=0;i<poolSizes.length;i++){
				connectionsPerHost.add(Integer.parseInt(poolSizes[i].trim()));
			}
			
			String[] threadsAllowedToBlockForConnectionMultipliers = prop.getProperty("threadsAllowedToBlockForConnectionMultiplier").split(regex);	
			for(int i=0;i<threadsAllowedToBlockForConnectionMultipliers.length;i++){
				threadsAllowedToBlockForConnectionMultiplier.add(Integer.parseInt(threadsAllowedToBlockForConnectionMultipliers[i].trim()));
			}
			
			String[] connectTimeouts = prop.getProperty("connectTimeout").split(regex);	
			for(int i=0;i<connectTimeouts.length;i++){
				connectTimeout.add(Integer.parseInt(connectTimeouts[i].trim()));
			}
			
			String[] autoConnectRetrys = prop.getProperty("autoConnectRetry").split(regex);	
			for(int i=0;i<autoConnectRetrys.length;i++){
				autoConnectRetry.add(Boolean.parseBoolean(autoConnectRetrys[i].trim()));
			}
			
			String[] connSizes = prop.getProperty("connSize").split(regex);	
			for(int i=0;i<connSizes.length;i++){
				connSize.add(Integer.parseInt(connSizes[i].trim()));
			}
			is.close();
		}catch (Exception e) {
			e.printStackTrace();
			throw new ExceptionInInitializerError("mongodb server init arguments init error!");
		}
	}

	public static List<String> getDbServerIP() {
		return dbServerIP;
	}

	public static List<Integer> getDbServerPort() {
		return dbServerPort;
	}

	
	public static List<String> getDbName() {
		return dbName;
	}

	public static List<String> getDbUserName() {
		return dbUserName;
	}

	public static List<String> getDbPassword() {
		return dbPassword;
	}

	public static List<Integer> getConnectionsPerHost() {
		return connectionsPerHost;
	}

	public static List<Integer> getThreadsAllowedToBlockForConnectionMultiplier() {
		return threadsAllowedToBlockForConnectionMultiplier;
	}

	public static List<Integer> getConnectTimeout() {
		return connectTimeout;
	}

	public static List<Boolean> isAutoConnectRetry() {
		return autoConnectRetry;
	}

	public static List<Integer> getConnSize() {
		return connSize;
	}
	
}
